import cv2
import math
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.resizeWindow('img',(640,480))
img=cv2.imread(r"D:\Project\Project37\Project37\tupian\angle.png")
points=[]
def mousepoint (event,x,y,flags,params):
    if (event==cv2.EVENT_LBUTTONDOWN):
       
        size=len(points)
        if size!=0 and size%3!=0:
            cv2.line(img,tuple(points[round((size-1)/3)*3]),(x,y),(255,0,0),2)
        cv2.circle(img,(x,y),2,(255,0,0),-1)
        points.append([x,y])
def gradiant(pt1,pt2):
    return (pt2[1]-pt1[1])/(pt2[0]-pt1[0])
def getangle(points):
    pt1,pt2,pt3=points[-3:]
    m1=abs(gradiant(pt1,pt2))
    m2=abs(gradiant(pt1,pt3))
    angle=(m2-m1)/(1+(m1*m2))
    angR=math.atan(angle)
    angD=abs(round(math.degrees(angR)))
    cv2.putText(img,str(angD),(pt1[0]-40,pt1[1]-20),cv2.FONT_HERSHEY_COMPLEX,1.5,(0,0,255),2)
    print(abs(angD))
while True:
    cv2.imshow('img',img)
    cv2.setMouseCallback('img',mousepoint)
    if len(points)%3==0 and len(points)!=0:
        getangle(points)
        
    key=cv2.waitKey(1)
    if key&0xFF==ord('q'):
       points=[]
    elif key&0xFF==27:
        break
cv2.destroyAllWindows()

